package com.zhiyou.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.zhiyou.mapper.UserMapper;
import com.zhiyou.model.Result;
import com.zhiyou.model.User;
import com.zhiyou.service.UserService;
@Service
public class UserServiceImpl implements UserService{
	//UserMapper  接口  需要实现类 bean ；从容器中找。
	//@Autowired 会从容器中查找，找到了，则注入； 找不到，userMapper 就是null
	//UserMapper接口 有可能又很多实现类；可以根据id进行查找
	//@Qualifier("对象的id")
	@Autowired
	private UserMapper userMapper=null;
	public UserMapper getUserMapper() {
		return userMapper;
	}
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	@Override
	public Result insertUser(User user) {
		//userMapper 必须要实例化，才可以调用方法
		//谁帮我们进行了实例化？ spring 扫描包，把所有的bean实例化，放到容器中。
		int i=userMapper.insertUser(user);//null.insertUser();  空指针异常
		Result result=new Result(false, "新增user失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("新增user成功");
		}
		return result;
	}
	
	
	@Override
	public User selectById(int id) {
		return userMapper.selectById(1);
	}
	@Override
	public Result updateUserById(User user) {
		int i=userMapper.updateUserById(user);
		Result result=new Result(false, "修改user失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("修改user成功");
		}
		return result;
	}
	
	

}
